package com.refuel.controller;

import com.refuel.domain.Order;
import com.refuel.enumeration.OrderStatus;
import com.refuel.request.OrderRequest;
import com.refuel.response.BaseResponse;
import com.refuel.response.PageResponse;
import com.refuel.response.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author zhanghao
 * @description 订单说明配置器
 * @date 2024/5/9 16:08
 **/
@RestController
@RequestMapping("/order")
@Tag(name = "订单控制器", description = "订单相关接口")
public class OrderController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    /**
     * 下订单
     * @return BaseResponse
     */
    @Operation(summary = "下单接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.POST, path = "/submit")
    public BaseResponse<String> submit(@RequestBody OrderRequest orderRequest) {
        logger.info("guide orderRequest: {}", orderRequest);

        String payInfo = "生成带签名的支付信息";
        return new BaseResponse<>(payInfo);
    }

    /**
     * 我的订单列表接口
     * @return PageResponse
     */
    @Operation(summary = "我的订单列表接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/list")
    public PageResponse<Order> orderList(@RequestParam(name = "uid") String uid) {
        logger.info("guide uid: {}", uid);
        PageResponse<Order> response = new PageResponse<>();

        response.addData(buildOrder("123456789"));
        return response;
    }

    /**
     * 支付回调
     * @return BaseResponse
     */
    @Operation(summary = "支付回调")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.POST, path = "/pay/callback")
    public BaseResponse<String> payCallback() {

        String callback = "pay callback success";
        return new BaseResponse<>(callback);
    }

    /**
     * 退款
     * @return BaseResponse
     */
    @Operation(summary = "退款")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.POST, path = "/refunded")
    public Response refunded(@RequestParam("uid") String uid,
                             @RequestParam("orderId") String orderId) {
        logger.info("refunded uid: {}, orderId: {}", uid, orderId);

        return new Response();
    }

    private Order buildOrder(String orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setCreateTime(new Date());
        order.setPrice(9.70);
        order.setGoodsId("2000001");
        order.setPayTime(new Date());
        order.setStatus(OrderStatus.PAY_OK.getName());

        return order;
    }
}
